package com.steel.yfmall.cloud.assistant.oms.impl;

import com.steel.yfmall.cloud.assistant.dto.para.oms.ParaMasterOrderInfo;
import com.steel.yfmall.cloud.assistant.oms.MasterOrderInfoService;
import com.steel.yfmall.cloud.assistant.oms.mapper.MasterOrderInfoMapper;
import com.steel.yfmall.cloud.assistant.po.oms.MasterOrderInfo;
import com.steel.yfmall.cloud.assistant.po.oms.MasterOrderInfoExample;
import com.steel.yfmall.cloud.assistant.po.oms.MasterOrderInoExtend;
import com.steel.yfmall.cloud.components.utils.utils.UtilDate;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 龙飞
 * @version 1.0
 * @since 2021/01/08 星期五 15:14
 */

@Service
public class MasterOrderInfoServiceImpl implements MasterOrderInfoService {

    @Override
    public List<MasterOrderInfo> selectByMasterOrderSnList(ParaMasterOrderInfo paraBean) {
        paraBean.setOrderStatus(3);
        MasterOrderInfoExample example = getConditionDate(paraBean);
        if(example == null || CollectionUtils.isEmpty(example.getOredCriteria())){
            return null;
        }
        return masterOrderInfoMapper.selectByExample(example);
    }

    /**
     * 查询订单和订单扩展信息
     * @Title: selectBeanInoByPk
     * @Description: (这里用一句话描述这个方法的作用)
     * @Param masterOrderSn 参数说明
     * @return com.steel.yfmall.cloud.assistant.po.oms.MasterOrderInoExtend    返回类型
     * @author yuchaohua
     * @date 2021/1/20 15:44
     */
    @Override
    public MasterOrderInoExtend selectBeanInoByPk(String masterOrderSn) {
        //return masterOrderInfoMapper.selectBeanInoByPk(masterOrderSn);
        return null;
    }

    @Override
    public int updateByPrimaryKeySelective(MasterOrderInfo masterOrderInfo) {
        masterOrderInfo.setUpdateTime(UtilDate.getDate());
        return masterOrderInfoMapper.updateByPrimaryKeySelective(masterOrderInfo);
    }


    /**
     * 需改 订单主信息上的 订单结算总价、实际商品结算重量
     *
     * @param paraMasterOrderInfo
     * @return true成功、false失败
     * @author jia chang bao
     */
    @Override
    public Boolean updateMasterOrderInfo(ParaMasterOrderInfo paraMasterOrderInfo) {
        MasterOrderInfoExample conditionDate = getConditionDate(paraMasterOrderInfo);
        if(conditionDate == null || CollectionUtils.isEmpty(conditionDate.getOredCriteria())){
            return false;
        }
        MasterOrderInfo masterOrderInfo = new MasterOrderInfo();
        masterOrderInfo.setClearGoodsWeight(paraMasterOrderInfo.getClearGoodsWeight());
        masterOrderInfo.setTotalSettlementPrice(paraMasterOrderInfo.getTotalSettlementPrice());
        return masterOrderInfoMapper.updateByExampleSelective(masterOrderInfo,conditionDate)>0?true:false;
    }

    /**
     * 查询单条订单信息
     * @Title: selectMasterOrderInfoByPara
     * @Description: (这里用一句话描述这个方法的作用)
     * @Param paraBean 参数说明
     * @return com.steel.yfmall.cloud.assistant.po.oms.MasterOrderInfo    返回类型
     * @author yuchaohua
     * @date 2021/1/14 21:35
     */
    @Override
    public MasterOrderInfo selectMasterOrderInfoByPara(ParaMasterOrderInfo paraBean) {
        MasterOrderInfoExample conditionDate = getConditionDate(paraBean);
        if(conditionDate == null || CollectionUtils.isEmpty(conditionDate.getOredCriteria())){
            return null;
        }
        return masterOrderInfoMapper.selectOneByExampleSelective(conditionDate);
    }

    /**
     * 根据条件修改订单信息
     * @Title: updateMasterOrderInfoByPara
     * @Description: (这里用一句话描述这个方法的作用)
     * @Param recordBean 参数说明
     * @Param paraBean 参数说明
     * @return boolean    返回类型
     * @author yuchaohua
     * @date 2021/1/22 10:32
     */
    @Override
    public boolean updateMasterOrderInfoByPara(MasterOrderInfo recordBean, ParaMasterOrderInfo paraBean) {
        MasterOrderInfoExample example = getConditionDate(paraBean);
        if(recordBean == null || example == null || CollectionUtils.isEmpty(example.getOredCriteria())){
            return false;
        }
        recordBean.setUpdateTime(UtilDate.getDate());
        return masterOrderInfoMapper.updateByExampleSelective(recordBean,example)>0;
    }

    /**
     * 条件
     *
     * @param paraMasterOrderInfo
     * @return
     * @author jia chang bao
     */
    private MasterOrderInfoExample getConditionDate(ParaMasterOrderInfo paraMasterOrderInfo) {
        MasterOrderInfoExample masterOrderInfoExample = new MasterOrderInfoExample();
        MasterOrderInfoExample.Criteria criteria = masterOrderInfoExample.createCriteria();
        if (ObjectUtils.isEmpty(paraMasterOrderInfo)) {
            return masterOrderInfoExample;
        }
        //主订单号 主键
        if (StringUtils.isNotBlank(paraMasterOrderInfo.getMasterOrderSn())) {
            criteria.andMasterOrderSnEqualTo(paraMasterOrderInfo.getMasterOrderSn());
        }
        // 订单号集合
        if (!CollectionUtils.isEmpty(paraMasterOrderInfo.getMasterOrderSnList())){
            criteria.andMasterOrderSnIn(paraMasterOrderInfo.getMasterOrderSnList());
        }
        //下单公司编码
        if (StringUtils.isNotBlank(paraMasterOrderInfo.getCompanyCode())) {
            criteria.andMasterOrderSnEqualTo(paraMasterOrderInfo.getCompanyCode());
        }
        //外部-中冠合同编号
        if (StringUtils.isNotBlank(paraMasterOrderInfo.getOutContractSn())) {
            criteria.andOutContractSnEqualTo(paraMasterOrderInfo.getOutContractSn());
        }
        // 订单版本批次号
        if (paraMasterOrderInfo.getVersion() != null){
            criteria.andVersionEqualTo(paraMasterOrderInfo.getVersion());
        }
        // 订单状态 0，未确认；1，已确认；2，已取消；3，完成；
        if (paraMasterOrderInfo.getOrderStatus() != null){
            criteria.andOrderStatusEqualTo(paraMasterOrderInfo.getOrderStatus());
        }
        // 排序条件拼接
        if (StringUtils.isNotBlank(paraMasterOrderInfo.getOrderByValue())) {
            masterOrderInfoExample.setOrderByClause(paraMasterOrderInfo.getOrderByValue());
        }
        return masterOrderInfoExample;
    }


    @Resource
    private MasterOrderInfoMapper masterOrderInfoMapper;
}
